package artid.com.arttogo.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.bean.ArtComment;
import artid.com.arttogo.bean.ArtistFollow;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.task.CallbackSky;
import artid.com.arttogo.utils.VeDate;

@SuppressLint("NewApi")
public class ArtListDetailAdapter extends BaseAdapter {
    private Context mContext;
    LayoutInflater mInflater;
    ViewHolder holder;
    CallbackSky cs;
    List<View> listIcon;
    public List<ArtComment> listdata;

    // private ImageLoader mImageLoader;

    public ArtListDetailAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
        listIcon = new ArrayList<View>();
        listdata = new ArrayList<ArtComment>();
        // RequestQueue mQueue = Volley.newRequestQueue(mContext);
        // mImageLoader = new ImageLoader(mQueue, new BitmapCache());
    }

    public void setdata(List<ArtComment> listdata) {
        this.listdata = listdata;
        notifyDataSetChanged();
    }

    public void addList(List<ArtComment> listdata) {
        this.listdata.addAll(listdata);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return listdata.size();
    }

    @Override
    public View getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_artdetail_comment,
                    null);
            vh.texttime = (TextView) convertView
                    .findViewById(R.id.artdet_cmt_time);
            vh.textdetail = (TextView) convertView
                    .findViewById(R.id.art_det_msg);
            vh.iv = (ImageView) convertView.findViewById(R.id.artdet_icon);
            vh.textname = (TextView) convertView.findViewById(R.id.artdet_name);
            vh.like = (ImageView) convertView.findViewById(R.id.artdet_like);
            vh.textreply = (TextView) convertView.findViewById(R.id.art_det_reply);
            vh.replytag = (TextView) convertView.findViewById(R.id.art_det_reply2);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        final ArtComment item = listdata.get(position);

        if (item.getImgurl().contains("http")) {

            Glide.with(mContext).load(item.getImgurl()).asBitmap()
                    .error(R.drawable.ic_menu_rotate)// load失敗的Drawable
                    .placeholder(R.drawable.ic_menu_rotate)// loading時候的Drawable
                    .centerCrop().into(vh.iv);

        }
        if (item.getIs_commend().equals("1")) {
            vh.like.setBackground(ContextCompat.getDrawable(mContext,
                    R.drawable.ysp_btn_good_sel));
        } else {
            vh.like.setBackground(ContextCompat.getDrawable(mContext,
                    R.drawable.ysp_btn_good));
        }

        vh.texttime.setText(VeDate.getStrMDTime(item.getCreate_time()));

        vh.textdetail.setText(item.getContent());

        vh.textname.setText(item.getRealname());
        vh.like.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (item.getIs_commend().equals("1")) {
                    reqArtReplycomment(item.getMessid(), position, false);

                } else {
                    reqArtReplycomment(item.getMessid(), position, true);

                }

            }
        });
        if (!TextUtils.isEmpty(item.getReplyname())) {
            vh.textreply.setText("@" + item.getReplyname());
            vh.replytag.setVisibility(View.VISIBLE);
//			String content = item.get
        }
        return convertView;
    }

    void reqArtReplycomment(String id, final int position, final Boolean isLike) {
        String state = "";
        if (isLike) {
            state = "1";
        } else {
            state = "2";
        }
        VolleyBasic.getInstance((Activity) mContext).post(Cache.zxcommentCommend,
                VolleyMap.getInstance().commentCommend(id, state),
                ArtistFollow.class, new VolleyCallback() {
                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        if (vd.getCode() == 1) {
                            if (!isLike) {
                                listdata.get(position).setIs_commend("0");
                            } else {
                                listdata.get(position).setIs_commend("1");
                            }

                            notifyDataSetChanged();
                        }

                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub

                    }
                });

    }

    class ViewHolder {
        TextView textdetail, texttime, textname, textreply, replytag;
        ImageView iv, like;

    }
}
